$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_tcxy_m")
    const dgrid1 = $NG.getCmpApi("p_form_tcxy_m.")
    const getHeadValue = (str) => mstform.getItem(str).getValue()


    if (page.oType == 'add') {
        getCntCbInfoByPhid()
    }

    useValuesChange(() => {
        getCntCbInfoByPhid()
    }, "p_form_tcxy_m.u_fbhtbh")

    useValuesChange(() => {
        getProjectInfoByPhid()
    }, "p_form_tcxy_m.u_fbhtbh")

    useValuesChange(() => {
        getProjectInfoByPhid()
    }, "p_form_tcxy_m.phid_pc")

    // 获取分包合同信息
    function getCntCbInfoByPhid() {
        const phid = getHeadValue("u_fbhtbh")
        if (!phid) {
            return
        }
        $NG.execServer("getCntCbInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            const arr = [
                {
                    key: "u_fbhtmc",
                    value: datas.cnt_name
                },
                {
                    key: "u_fbhtqdrq",
                    value: datas.user_htnqdsj
                },
                {
                    key: "u_fbhtbh",
                    value: {
                        label: datas.cnt_no,
                        value: datas.phid
                    }
                },
                {
                    key: "u_fbhtje",
                    value: datas.user_fbhtje
                },
                {
                    key: "u_fbsmc",
                    value: {
                        value: datas.fbsdw_phid,
                        label: datas.fbsdw_name
                    }
                },
                {
                    key: "u_fbf",
                    value: {
                        label: datas.fbfmc_name,
                        value: datas.fbsmc_phid
                    }
                },
                {
                    key: "u_htkgsj",
                    value: datas.user_htjhkssj
                },
                {
                    key: "u_htwgsj",
                    value: datas.user_htjhjssj
                },
                {
                    key:"phid_pc",
                    value:{
                        label:datas.phid_pc_exname,
                        value:datas.phid_pc
                    }
                }
            ]
            getSupplyInfo(datas.fbsdw_phid)
            setHeadValue(arr)
        })
    }

    // 获取分包商单位
    function getSupplyInfo(phid) {
        if (!phid) return
        $NG.execServer("getSupplyInfo", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            const arr = [
                {
                    key: "u_fbsdz",
                    value: datas.taxaddress
                },
                {
                    key: "u_fbszrr",
                    value: datas.person
                },
                {
                    key: "u_fbslxr",
                    value: datas.person
                },
               
                {
                    key: "u_lxdh",
                    value: datas.user_fddbrlxdh
                }
            ]
            setHeadValue(arr)
        })
    }

    //获取项目信息
    function getProjectInfoByPhid() {
        const phid = mstform.getItem("phid_pc").getValue()
        if (!phid) return
        $NG.execServer("getProjectInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            mstform.getItem("u_fbfzrr").setValue({
                label: datas.project_manager_name,
                value: datas.project_manager
            })
        
        })
    }

    // 赋值
    function setHeadValue(datas) {
        datas.forEach(item => {
            try {
                mstform.getItem(item.key).setValue(item.value)
            } catch (err) {
                console.log(`err`, err, item);
            }
        })
    }
})